/// ===========================================================================
/// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
/// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
/// PURPOSE.
/// ===========================================================================
/// 
/// Project:        MOSS Faceted Search
/// Author:         Leonid Lyublinski (leonidly@microsoft.com)
/// Company:        Microsoft Services
/// Date:           09/17/2007  Version:        1.1
///
/// ===========================================================================

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.SharePoint.Portal.ExtendedSearch
{

    internal class FacetMenu : List<FacetCollection>, IComparer<FacetCollection>
    {
        #region Fields
        private FacetSortEnum _sortOrder = FacetSortEnum.Name;
        private int _hits;
        #endregion

        #region Properties
        public FacetSortEnum SortOrder
        {
            get { return _sortOrder; }
            set { _sortOrder = value; }
        }
        public int Hits
        {
            get
            {
                _hits = 0;
                for (int i = 0; i < Count; i++)
                {
                    _hits += this[i].Hits;
                }
                return _hits;
            }
        }
        #endregion

        #region Methods
        public bool ContainsFacetCollection(string facetCollectionName)
        {
            for (int i = 0; i < Count; i++)
            {
                if (this[i].Name == facetCollectionName) return true;
            }
            return false;
        }
        #endregion

        #region Constructors
        public FacetMenu()
            : base()
        { }

        public FacetMenu(Dictionary<string, Dictionary<string, Int32>> facetMenu)
            : this()
        {
            foreach (string facetCollectionName in facetMenu.Keys)
            {
                FacetCollection facetCollection = new FacetCollection(facetCollectionName);
                this.Add(facetCollection);

                Dictionary<string, int> facets = facetMenu[facetCollectionName];
                foreach (string facetName in facets.Keys)
                {
                    Facet facet = new Facet(facetName, facets[facetName]);
                    facetCollection.Add(facet);
                }
            }
        }

        #endregion

        #region IComparer<FacetCollection> Members

        public int Compare(FacetCollection x, FacetCollection y)
        {
            switch (_sortOrder)
            {
                case FacetSortEnum.Name:
                    return x.Name.CompareTo(y.Name);

                case FacetSortEnum.Hits:
                    return -1 * x.Hits.CompareTo(y.Hits); // by default DESC

                case FacetSortEnum.Max:
                    return -1 * x.Max.CompareTo(y.Max); // by default DESC
                
                default:
                    return 0;
            }
        }

        #endregion
    }

    internal class FacetCollection : List<Facet>, IComparer<Facet>
    {
        #region Fields
        private FacetSortEnum _sortOrder = FacetSortEnum.None;
        private string _name;
        private int _hits;
        private int _max;

        #endregion

        #region Properties
        public int Hits
        {
            get
            {
                _hits = 0;
                for (int i = 0; i < Count; i++)
                {
                    _hits += this[i].Hits;
                }
                return _hits;
            }
        }
        public int Max
        {
            get
            {
                for (int i = 0; i < Count; i++)
                {
                    _max = Math.Max(_max, this[i].Hits);
                }
                return _max;
            }
        }
        public FacetSortEnum SortOrder
        {
            get { return _sortOrder; }
            set { _sortOrder = value; }
        }
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        #endregion

        #region Methods
        public bool ContainsFacet(string facetName)
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (this[i].Name == facetName) return true;
            }
            return false;
        }
        #endregion

        #region Constructors
        public FacetCollection()
            : base()
        { }

        public FacetCollection(string name)
            : this()
        {
            _name = name;
        }
        #endregion

        #region IComparer<Facet> Members
        public int Compare(Facet x, Facet y)
        {
            switch (_sortOrder)
            {
                case FacetSortEnum.Name:
                    return x.Name.CompareTo(y.Name);

                case FacetSortEnum.Hits:
                    return -1 * x.Hits.CompareTo(y.Hits); // by default DESC       

                default:
                    return 0;
            }

        }

        #endregion
    }

    internal class Facet
    {
        #region Fields
        private string _name;
        private int _hits;
        #endregion

        #region Properties
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public int Hits
        {
            get { return _hits; }
            set { _hits = value; }
        }
        #endregion

        #region Constructors
        public Facet(string name, int count)
        {
            _name = name;
            _hits = count;
        }
        public Facet(DictionaryEntry entry)
        {
            _name = entry.Key.ToString();
            _hits = (int)entry.Value;
        }
        #endregion
    }

    public enum FacetSortEnum
    {
        None,
        Name,
        Hits,
        Max
    }
}
